
function User(username, email, firstname, lastname, bornDate, created, lastLogin, roles){
	this.username = username;
	this.email = email;
	this.firstname = firstname;
	this.lastname = lastname;
	this.bornDate = bornDate;
	this.created = created;
	this.lastLogin = lastLogin;
	this.roles = roles;
};

function Friend(visible, friend){
	this.visible = visible;
	this.friend = friend;
};

FriendsManager = {

		friendImage : new google.maps.MarkerImage('resources/css/img/icons/znajomy.png',
				// This marker is 20 pixels wide by 32 pixels tall.
				new google.maps.Size(32, 32),
				// The origin for this image is 0,0.
				new google.maps.Point(0, 0),
				// The anchor for this image is the base of the flagpole at 0,32.
				new google.maps.Point(16, 16)),

		friendShape : {
			coord : [ 0,0, 32,32 ],
			type : 'rect'
		},
		
		markerImage : new google.maps.MarkerImage('resources/css/img/icons/ogolna.png',
				// This marker is 20 pixels wide by 32 pixels tall.
				new google.maps.Size(32, 32),
				// The origin for this image is 0,0.
				new google.maps.Point(0, 0),
				// The anchor for this image is the base of the flagpole at 0,32.
				new google.maps.Point(16,16)),

		markerShape : {
			coord : [ 0,0, 32,32 ],
			type : 'rect'
		},
		
		map : null,
		
		interval : 15000,
		
		intervalSet : false,
		
		trackedFriends : [],
		
		friends : [],
		
		friendsOnline : [],
		
		//friendsMarkers
		friendsList : [],
		
		friendsMarkersList : [],
		
		markers : [],
		
		infoWindows : [],
		
		initialize : function(map){
			FriendsManager.map = map;
			
			FriendsManager.getTrackedFriends();
			
			FriendsManager.getFriendsList();
			
			if (!FriendsManager.intervalSet) {
				window.setInterval(function() {
					FriendsManager.getTrackedFriends();
				
					
				}, FriendsManager.interval);
				FriendsManager.intervalSet = true;
			}
			
			$("#showFriends").bind('click', function(event){
				FriendsManager.showFriends();
			});
			
			$("#hideFriends").bind('click', function(event){
				FriendsManager.hideFriends();
			});
			
			$("#onlineFriendsList ul li input").die('click').live('click', function(){
				var checked = $(this).attr('checked');
				var username = $(this).val();
				$.each(FriendsManager.friendsOnline, function(key, val) {
					if(username == val.friend.username)
					{
						if(checked == "checked")
						{
							val.visible = true;
							FriendsManager.showFriends();
						}
						else
						{
							val.visible = false;
							FriendsManager.hideFriend(val.friend);
						}
					}
				});
				
			});
			
			$("#friendsMarkersList ul li input").die('click').live('click', function(){
				var checked = $(this).attr('checked');
				var username = $(this).val();
				$.each(FriendsManager.friendsMarkersList, function(key, val) {
					if(username == val.author)
					{
						if(checked == "checked")
						{
							val.visible = true;
							FriendsManager.showMarkers(val);
						}
						else
						{
							val.visible = false;
							FriendsManager.hideMarkers(val);
						}
					}
				});
				
			});
			
		},
		
		getTrackedFriends : function(){
			Api.me.getTrackedFriends(function(data){
				FriendsManager.trackedFriends = [];
				
				var temp = FriendsManager.friendsOnline;
				FriendsManager.friendsOnline = [];
				
				if(data.length > 0)
				{
					$("#onlineFriendsList ul li").remove();
					
					var i = 0;
					
					$.each(data, function(key, val) {
						var friend = new User(val.username, val.email, val.firstname, val.lastname, val.bornDate, val.created, val.lastLogin, val.roles);
						FriendsManager.trackedFriends.push(friend);
						
						FriendsManager.updateFriend(friend);
						
						if(temp.length > 0)
						{
							$.each(temp, function(key,val){
								if(val.friend.username == friend.username)
								{
									var friendOnline = new Friend(val.visible, val.friend);
									FriendsManager.friendsOnline.push(friendOnline);
									var checked = null;
									if(val.visible)
										checked = "checked";
									$("#friendsList-online").append('<li class="ui-state-default">' + val.friend.username + ' <input class="cb" type="checkbox" value="' + val.friend.username + '" style="float:right;" ' + checked + ' /></li>');
								}
							});
							
						}
						else
						{
							var friendOnline = new Friend(false, friend);
							FriendsManager.friendsOnline.push(friendOnline);
							$("#friendsList-online").append('<li class="ui-state-default">' + val.username + ' <input class="cb" type="checkbox" value="' + val.username + '" style="float:right;" /></li>');
						}
						i++;			
					});
				}
				$(".friendsCount").text(data.length);
				
			});
		},
		
		getFriendsList : function(){
			Api.me.getFriends(function(data){
				FriendsManager.friendsList = [];
				
				FriendsManager.friendsMarkersList = [];
				
				if(data.length > 0)
				{
					$("#friendsMarkersList ul li").remove();
					
					var i = 0;
					
					$.each(data, function(key, val) {
						var friend = new User(val.username, val.email, val.firstname, val.lastname, val.bornDate, val.created, val.lastLogin, val.roles);
						FriendsManager.friendsList.push(friend);					

						FriendsManager.updateMarkersList(friend);
						
						// var friendMarker = new Friend(false, friend);
						// FriendsManager.friendsMarkersList.push(friendMarker);
						$("#friendsList-markers").append('<li class="ui-state-default">' + val.username + ' <input class="cb" type="checkbox" value="' + val.username + '" style="float:right;" /></li>');
						i++;			
					});
				}
				
			});
		},
		
		updateFriend : function(user) {
			Api.me.getUserLocalization(user.username, function(data){

				var myLatLng = new google.maps.LatLng(data.latitude,
						data.longitute);
				
				var marker = null;
				
				$.each(FriendsManager.friends, function(key, val) {
					if(val.title == user.username)
					{
						marker = val;
					}
				});
				
				if(marker == null)
				{
					var map = null;
					
					marker = new google.maps.Marker({
						position : myLatLng,
						map : map,
						// shadow : shadow,
						icon : FriendsManager.friendImage,
						shape : FriendsManager.friendShape,
						title : data.user.username,
						zIndex : data.user.username.length,
					});
					
					FriendsManager.friends.push(marker);
				}
				else
				{
					marker.setPosition(myLatLng);
				}
			});
		},
		
		showFriends : function(){
			if(FriendsManager.friends.length > 0)
			{
				var friendsOnline = [];
				
				$("#onlineFriendsList ul li :checkbox:checked").each(
					    function() {
					    	friendsOnline.push($(this).val());
					        // $(this).attr('checked', true);
					    }
				);
				
				for( var i = 0; i < FriendsManager.friends.length; i++)
				{
					$.each(friendsOnline, function(key, val){
						if(val == FriendsManager.friends[i].title)
							FriendsManager.friends[i].setMap(FriendsManager.map);	
					});
				}
			}
		},
		
		hideFriends : function(){
			if(FriendsManager.friends.length > 0)
			{
				for( var i = 0; i < FriendsManager.friends.length; i++)
					FriendsManager.friends[i].setMap(null);
			}
		},
		
		hideFriend : function(friend){
			if(FriendsManager.friends.length > 0)
			{
				for( var i = 0; i < FriendsManager.friends.length; i++)
				{
					if(friend.username == FriendsManager.friends[i].title)
						FriendsManager.friends[i].setMap(null);
				}
			}
		},
		
		updateMarkersList : function(friend){
			Api.me.getFriendMarkers(friend.username, function(data){
				
				if(data.length > 0)
				{
					$.each(data, function(key, val) {
						var friendMarker = new userMarker(val.id, val.name, val.address,
								val.description, val.longitude, val.latitude, val.type,
								val.author, false);
						FriendsManager.friendsMarkersList.push(friendMarker);
					});
				}
				
				FriendsManager.updateMarkers();
			});
		},
		
		updateMarkers : function(){
			for( var i = 0; i < FriendsManager.friendsMarkersList.length; i++)
			{
				FriendsManager.updateMarker(FriendsManager.friendsMarkersList[i]);
			};
		},
		
		showMarkers : function(friendMarker){

			if(friendMarker.visible == true)
			{
				for( var j = 0; j < FriendsManager.markers.length; j++)
				{
					if(friendMarker.id == FriendsManager.markers[j].getZIndex())
						FriendsManager.markers[j].setMap(FriendsManager.map);	
				}
			}

		},
		
		hideMarkers : function(friendMarker){

			if(friendMarker.visible != true)
			{
				for( var j = 0; j < FriendsManager.markers.length; j++)
				{
					if(friendMarker.id == FriendsManager.markers[j].getZIndex())
						FriendsManager.markers[j].setMap(null);	
				}
			}
		},
		
		updateMarker : function(friendMarker){
			var marker = null;
			var index = -1;
			
			for( var i = 0; i < FriendsManager.markers.length; i++)
			{
				if(FriendsManager.markers[i].id == friendMarker.id)
				{
					marker = FriendsManager.markers;
					index = i;
				}
			};
			
			if(marker == null)
			{
				function infoCallback(infowindow, marker) {
					return function() {

						if (MarkerManager.currentInfoWindow != null)
							MarkerManager.currentInfoWindow.close();

						infowindow.open(MarkerManager.map, marker);
						MarkerManager.currentInfoWindow = infowindow;
					};
				}
				
				var latLng = new google.maps.LatLng(friendMarker.latitude,
						friendMarker.longitude);
				
				var marker = new google.maps.Marker({
					position : latLng,
					map : null,
					// shadow : shadow,
					icon : MarkerTypes.getIcon(friendMarker.type),
					shape : FriendsManager.markerShape,
					title : friendMarker.name,
					zIndex : friendMarker.id
				});
				
				FriendsManager.markers.push(marker);
				
				var contentDiv = "<div class=\"infoWindow\"> ID: " + friendMarker.id
				+ "<br><h3>" + friendMarker.name + "</h3><p>"
				+ friendMarker.description + '</p>' +
				"<br><p>Autor: <b>" + friendMarker.author + '</b></p>' + 
				'<div class="clear"></div></div>';
				
				var infowindow = new google.maps.InfoWindow({
					content : contentDiv
				});
		
				FriendsManager.infoWindows.push(infowindow);
				
				google.maps.event.addListener(marker, 'click', infoCallback(
						infowindow, marker, FriendsManager.map));
			}
		}
};



